/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gblend.adjlistgraph;

/**
 *
 * @author ibmuser
 */
public class Link {

    private Node first=null;
    private Node current=null;
 
    public void add(int index)
    {
       Node  node = new Node(index);
       node.next(first);
       first = node;
     }

    public boolean hasNext()
    {
         return current == null? first != null: current.next() != null;
     }

    public Node next()
    {
         if(current == null)
             current = first;
         else
             current = current.next();
         return current;
     }

  public  void reset()
  {
      current = null;
  }

   public  void remove(int index)
   {
         Node previous = null;
         Node current = first;
         while(current != null) {
             if (current.index() == index) {
                 if(previous == null) first = current;
                 else previous.next(current.next());
                 return;
             }
             previous = current;
             current = current.next();
         }
     }

     public Node first()
     {
         return first;
     }
}
